#coding=utf-8
import requests,json,urllib
import mysql_password

#朵云社区登录接口并返回带token的headers
def login(mobile):
    #通过mysql_password方法获取到该用户的salt加密后的密码
    password = mysql_password.password(mobile)
    s = requests.session()
    mobile_panduan = s.get('http://community-platform-api-test.gdgyun.com/api/community/account/check/mobile?mobile='+mobile)  #判断该号码是否注册
    login_data = ({"mobile":mobile,"encryptedPassword":password})
    login = s.post('http://community-platform-api-test.gdgyun.com/api/community/account/login',json=login_data,headers={'Content-Type':'application/json;charset=UTF-8'})  #进行登录操作
    token1 = login.json()["data"]["token"],login.text  #获取token
    userid = login.json()["data"]["userId"],login.text  #获取Uid
    token = str(token1[0])
    uid = str(userid[0])
    #定义headers
    headers = {'Accept': 'application/json, text/plain, */*','Accept-Encoding': 'gzip, deflate','Accept-Language': 'zh-CN,zh;q=0.9'
               ,'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1'
               ,'Referer': 'http://community-ui1-test.gdgyun.com/','Uid':uid,'Authorization': token,'Origin': 'http://community-ui1-test.gdgyun.com','Host': 'community-platform-api-test.gdgyun.com'
               ,'Content-Type':'application/json;charset=UTF-8'}
    #判断该账号状态
    if u'该用户账号已注册' in mobile_panduan.text:
        print u'该用户账号已注册'
    if u'该用户账号不存在' in mobile_panduan.text:
        print u'该用户账号不存在'
    if u'登录成功' in login.text:
        print u'登录成功'
        # index = s.get('http://community-platform-api-test.gdgyun.com/api/community/userInfo/view',headers=headers)  #进入朵云-个人中心页面
        # print index.text
    if u'该用户账号密码输入错误' in login.text:
        print u'该用户账号密码输入错误'
    #返回登录后带token的headers和Uid
    return headers,uid,token

if __name__ =='__main__':
    login('17781108472')